<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 顶部搜索框区域 -->
      <el-row :gutter="20">
        <!-- <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getTextList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getTextList"
            ></el-button>
          </el-input>
        </el-col> -->
        <el-col :span="2">
          <el-button type="primary" @click="gotoAddTests">添加文章</el-button>
        </el-col>
        <!-- <el-col :span="2">
          <el-button type="primary" @click="deleteTests">删除文章</el-button>
        </el-col> -->
      </el-row>
      <!-- 考试表格区域 -->
      <el-table
        :data="textList"
        height="450"
        stripe
        border
        @selection-change="handleSelectionChange1"
      >
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="articleTitle" label="文章"> </el-table-column>
        <el-table-column prop="author" label="作者" width="150px">
        </el-table-column>
        <el-table-column prop="articleRead" label="阅读量" width="100px">
        </el-table-column>
        <el-table-column prop="articleCollection" label="收藏量" width="100px">
        </el-table-column>
        <el-table-column prop="articleLike" label="喜爱量" width="100px">
        </el-table-column>
        <el-table-column
          prop="articleComment"
          label="评论数"
          width="100px"
        ></el-table-column>
        <el-table-column label="操作" width="220px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="gotoEditTests(scope.row)"
              >编辑</el-button
            >
            <!-- <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="gotoComment(scope.row)"
              >评论</el-button
            > -->
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              @click="deleteTest(scope.row.articleId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 15
      },
      total: 11,
      //   文章列表
      textList: [],
      // 删除考试/小测
      textDeleteIdList: []
    };
  },
  methods: {
    // 获取文章列表
    async getTextList() {
      const { data: res } = await this.$http.get(
        "article/preview/" + this.queryInfo.pagenum
      );
      console.log(res.data);
      if (res.code !== 200) return this.$message.error("获取文章列表失败！");
      //   this.total = res.data.total;
      // 文章列表
      this.total = res.data.total;
      this.textList = res.data.articleVoList;
    },
    // 多选勾选时将对应articleId添加到删除序列
    handleSelectionChange1(rows) {
      this.textDeleteIdList = [];
      rows.forEach(item => {
        this.textDeleteIdList.push(item.articleId);
      });
      console.log(this.textDeleteIdList);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getTextList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getTextList();
    },
    // 删除单个文章
    async deleteTest(articleId) {
      const deleteResult = await this.$confirm("是否删除该文章?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      const { data: res } = await this.$http.delete(
        "article/removeArticleById/" + articleId
      );
      if (res.code !== 200) return this.$message.error("删除文章失败！");
      console.log(res);
      this.$message.success("删除成功！");
      this.getTextList();
    },
    //   删除多个文章
    async deleteTests() {
      const deleteResult = await this.$confirm("是否删除选中文章?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err);
      if (deleteResult === "cancel") return this.$message.info("取消删除！");
      //   const { data: res } = await this.$http.delete("goods/" + id);
      //   if (res.meta.status !== 200) return this.$message.error("删除商品失败！");
      this.$message.success("删除成功！");
      this.getTextList();
    },
    // 跳转到添加考试组件
    gotoAddTests() {
      this.$router.push("/text/addtext");
    },
    // 跳转到编辑考试组件
    gotoEditTests(row) {
      window.sessionStorage.setItem("articleId", row.articleId);
      this.$router.push("/text/edittext");
    },
    // 跳转到评论组件
    gotoComment(row) {
      window.sessionStorage.setItem("articleId", row.articleId);
      this.$router.push("/text/comment");
    }
  },
  created() {
    this.getTextList();
  }
};
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
</style>